﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Web.Security;
using SMSBusiness;
using SMSDataAccess;

public partial class Account_login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public static UserProfile GetProfileBaseObj()
    {
        UserProfile profBase = (UserProfile)HttpContext.Current.Profile;
        return profBase;
    }
    public static General GetGeneralObj()
    {
        General GeneralObj = new General(GetProfileBaseObj().UserId);
        return GeneralObj;
    }
    protected void Loginbtn_Click(object sender, EventArgs e)
    {
        string UserId = txtname.Value.Trim();
        string Password = txtpw.Value.Trim();
        List<User> userInfoList = GetGeneralObj().GetUserInfo(UserId, Password);
        if (userInfoList.Count() > 0)
        {
            Dictionary<string, string> userInfo = new Dictionary<string, string>();
            userInfo.Add("UserId", userInfoList[0].User_Id);
            userInfo.Add("FirstName", userInfoList[0].First_Name);
            userInfo.Add("LastName", userInfoList[0].Last_Name);
            userInfo.Add("UserType", userInfoList[0].User_Type);


            if (!Membership.ValidateUser(UserId, Password.ToLower()))
            {
                try
                {
                    Membership.CreateUser(UserId, Password.ToLower());
                }
                catch
                {
                    ;
                }
            }

            UpdateProfile(userInfo);
            Response.Redirect("~/admin/DashBoard.aspx");
        }

    }

    private void UpdateProfile(Dictionary<string, string> userInfo)
    {

        MembershipUser mu = Membership.GetUser(userInfo["UserId"]);

        UserProfile up = ((UserProfile)ProfileBase.Create(mu.UserName));

        up.UserId = userInfo["UserId"];
        up.FirstName = userInfo["FirstName"];
        up.LastName = userInfo["LastName"];
        up.UserType = userInfo["UserType"];


    }
}